home *** CD-ROM | disk | FTP | other *** search
- #include "WriteExternalFile.h"
- #include "Tools.h"
- #include "MB_protos.h"
- #include "MB_pragmas.h"
- #include "mb.h"
-
- #include <exec/types.h>
- #include <clib/dos_protos.h>
- #include <clib/exec_protos.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- extern void Quit(void);
-
- /****************************************************************************************************************/
- /***** *****/
- /** WriteExternalFile **/
- /***** *****/
- /****************************************************************************************************************/
-
- /* Create a file where are the external variables and functions declarations */
- BOOL WriteExternalFile(char *Externals,ULONG varnb)
- {
- int i;
- ULONG length, type;
- BPTR TMPfile;
- char *adr_file = NULL;
- __aligned struct FileInfoBlock Info;
- BOOL bool_aux = FALSE;
- char *varname;
- char *tmp;
- BOOL result = FALSE;
- FILE *file;
- BOOL ExternalExist=FALSE;
-
- /* If the file already exists, we load it in memory */
- if (TMPfile = Open(Externals, MODE_OLDFILE))
- {
- ExamineFH(TMPfile, &Info);
- length = Info.fib_Size;
- if (!(adr_file = AllocMemory(length+1)))
- {
- Close(TMPfile);
- Quit();
- }
- Read( TMPfile, adr_file, length);
- adr_file[length] = '\0';
- Close(TMPfile);
- }
-
- for(i=0;!ExternalExist && i<varnb;i++)
- {
- MB_GetVarInfo(i,MUIB_VarType,&type,TAG_END);
- ExternalExist=(type==TYPEVAR_EXTERNAL || type==TYPEVAR_HOOK);
- }
-
- if (ExternalExist && (file = fopen(Externals, "a+")))
- {
- if (!TMPfile)
- fprintf(file,"#include <exec/types.h>\n\n");
-
- for(i=0;i<varnb;i++)
- {
- MB_GetVarInfo (i,
- MUIB_VarType, &type,
- MUIB_VarName, &varname,
- TAG_END
- );
- switch(type) /* if the declaration doesn't exist, we generate it */
- {
- case TYPEVAR_EXTERNAL:
- if (!(tmp=AllocMemory(strlen(varname)+14)))
- {
- fclose(file);
- Quit();
- }
- strcpy(tmp,"extern int ");
- strcat(tmp,varname);
- strcat(tmp,";\n");
- if (adr_file)
- bool_aux = (strstr(adr_file,tmp)!=NULL);
- if (!bool_aux)
- fprintf(file,tmp);
- FreeMemory(tmp);
- break;
-
- case TYPEVAR_HOOK:
- if (!(tmp=AllocMemory(strlen(varname)+52)))
- {
- fclose(file);
- Quit();
- }
- strcpy(tmp,"extern APTR ");
- strcat(tmp,varname);
- strcat(tmp,"( struct Hook *a0, APTR a2, APTR a1 );\n");
- if (adr_file)
- bool_aux = (strstr(adr_file,tmp)!=NULL);
- if (!bool_aux)
- fprintf(file,tmp);
- FreeMemory(tmp);
- break;
- }
- }
- fclose(file);
- }
- if (adr_file)
- FreeMemory(adr_file);
-
- if (TMPfile = Open(Externals, MODE_OLDFILE)) /* if the file is 0 bytes long : we remove it */
- {
- ExamineFH(TMPfile, &Info);
- Close(TMPfile);
- length = Info.fib_Size;
- if (length == 0)
- DeleteFile(Externals);
- else
- result = TRUE;
- }
- return(result);
- }
-